home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / mep2 / MemMon 2.0 / Samples / concord.dat < prev    next >
Encoding:
Text File  |  1990-11-25  |  17.4 KB  |  552 lines  |  [TEXT/PICN]

  1. .so tmac.tr
  2. .DA "January 1, 1990; last revised July 4, 1990"
  3. .TR 90-6b
  4. .Gl
  5. .TL
  6. An Overview of the Icon Programming Language; Version 8
  7. .AU
  8. Ralph E. Griswold
  9. .AE
  10. .tr *\(**
  11. .NH
  12. Introduction
  13. .PP
  14. Icon is a high-level programming language with extensive facilities for
  15. processing strings and structures. Icon has several novel features, including
  16. expressions that may produce sequences of results, goal-directed
  17. evaluation that automatically searches for a successful result, and
  18. string scanning that allows operations on strings to be formulated at
  19. a high conceptual level.
  20. .PP
  21. Icon emphasizes high-level string
  22. processing and a design philosophy that allows ease of programming
  23. and short, concise programs. Storage allocation and
  24. garbage collection are automatic in Icon, and there are few restrictions on the
  25. sizes of objects. Strings, lists, and other structures are created
  26. during program execution and their size does not need to be known when
  27. a program is written.
  28. Values are converted to expected types automatically; for example,
  29. numeral strings read in as input can be used in numerical computations
  30. without explicit conversion.
  31. Icon has an expression-based syntax with reserved words;
  32. in appearance, Icon programs resemble those of Pascal and C.
  33. .PP
  34. Although Icon has extensive facilities for processing strings and structures,
  35. it also has a full repertoire of computational facilities. It is suitable
  36. for a wide variety of applications.
  37. Some examples are:
  38. .in .5i
  39. .IP \(bu
  40. text analysis, editing, and formatting
  41. .IP \(bu
  42. document formating
  43. .IP \(bu
  44. artificial intelligence
  45. .IP \(bu
  46. expert systems
  47. .IP \(bu
  48. rapid prototyping
  49. .IP \(bu
  50. symbolic mathematics
  51. .IP \(bu
  52. text generation
  53. .IP \(bu
  54. data laundry
  55. .in 0
  56. .PP
  57. There are public-domain implementations of Icon for the Amiga, the Atari ST,
  58. the Macintosh under MPW, MS-DOS, MVS, OS/2, many UNIX systems, VM/CMS, and
  59. VMS. There also is a commercial implementation of an enhanced version of
  60. Icon for the Macintosh
  61. [.proicon.].
  62. .PP
  63. The remainder of this report briefly describes the highlights of Icon.
  64. For a complete description, see [.ilbook, .tr90-1.].
  65. .NH
  66. Expression Evaluation
  67. .NH 2
  68. Conditional Expressions
  69. .PP
  70. In Icon there are \fIconditional expressions\fR that may \fIsucceed\fR and
  71. produce a result, or may \fIfail\fR and not produce any result. An example
  72. is the comparison operation
  73. .Ds
  74. i > j
  75. .De
  76. which succeeds (and produces the value of \*Mj\fR) provided that the value
  77. of \*Mi\fR is greater than the value of \*Mj\fR, but fails otherwise.
  78. Similarly,
  79. .Ds
  80. i > j > k
  81. .De
  82. succeeds if the value of \*Mj\fR is between \*Mi\fR and \*Mk\fR.
  83. .PP
  84. The success or failure of conditional operations is used instead of
  85. Boolean values to drive control structures in Icon. An example is
  86. .Ds
  87. if i > j then k := i else k := j
  88. .De
  89. which assigns the value of \*Mi\fR to \*Mk\fR if the value of \*Mi\fR
  90. is greater than the value of \*Mj\fR, but assigns the value of \*Mj\fR to
  91. \*Mk\fR \%otherwise.
  92. .PP
  93. The usefulness of the concepts of success and failure is illustrated by
  94. \*Mfind(s1,s2)\fR, which fails
  95. if \*Ms1\fR does not occur as a substring of \*Ms2\fR.
  96. Thus
  97. .Ds
  98. if i := find("or",line) then write(i)
  99. .De
  100. writes the position at which \*M"or"\fR occurs in \*Mline\fR, if it occurs,
  101. but does not write a value if it does not occur.
  102. .PP
  103. Many expressions in Icon are conditional. An example is \*Mread()\fR,
  104. which produces the next line from the input file, but fails when the
  105. end of the file is reached. The following expression is typical of
  106. programming in Icon and illustrates the integration of conditional
  107. expressions and conventional control structures:
  108. .Ds
  109. while line := read() do
  110.    write(line)
  111. .De
  112. This expression copies the input file to the output file.
  113. .PP
  114. If an argument of a function fails, the function is not called,
  115. and the function call fails as well. This ``inheritance'' of failure allows the
  116. concise formulation of many programming tasks. Omitting the optional
  117. \*Mdo\fR clause in \*Mwhile-do\fR, the previous expression can be
  118. rewritten as
  119. .Ds
  120. while write(read())
  121. .De
  122. .NH 2
  123. Generators
  124. .PP
  125. In some situations, an expression may be capable of producing more than
  126. one result. Consider
  127. .Ds
  128. sentence := "Store it in the neighboring harbor"
  129. find("or",sentence)
  130. .De
  131. Here \*M"or"\fR occurs in \*Msentence\fR at positions 3, 23, and 33. Most
  132. programming languages treat this situation by selecting one of the
  133. positions, such as the first, as the result of the expression. In Icon,
  134. such an expression is a \fIgenerator\fR and is capable of producing
  135. all three positions.
  136. .PP
  137. The results that a generator produces depend on context. In a situation
  138. where only one result is needed, the first is produced, as in
  139. .Ds
  140. i := find("or",sentence)
  141. .De
  142. which assigns the value 3 to \*Mi\fR.
  143. .PP
  144. If the result produced by a generator does not lead to the success of
  145. an enclosing expression, however, the generator is \fIresumed\fR
  146. to produce another value. An example is
  147. .Ds
  148. if (i := find("or",sentence)) > 5 then write(i)
  149. .De
  150. Here the first result produced by the generator, 3, is assigned to
  151. \*Mi\fR, but this value is not greater than 5 and the comparison
  152. operation fails. At this point, the generator is resumed and
  153. produces the second position, 23, which is greater than 5. The
  154. comparison operation then succeeds and the value 23 is written.
  155. Because of the inheritance of failure and the fact that comparison
  156. operations return the value of their right argument, this expression
  157. can be written in the following more compact form:
  158. .Ds
  159. write(5 < find("or",sentence))
  160. .De
  161. .PP
  162. Goal-directed evaluation is inherent in the expression evaluation
  163. mechanism of Icon and can be used in arbitrarily complicated situations.
  164. For example,
  165. .Ds
  166. find("or",sentence1) = find("and",sentence2)
  167. .De
  168. succeeds if \*M"or"\fR occurs in \*Msentence1\fR at the same position
  169. as \*Mand\fR occurs in \*Msentence2\fR.
  170. .PP
  171. A generator can be resumed repeatedly to produce all its results by
  172. using the \*Mevery-do\fR control structure. An example is
  173. .Ds
  174. every i := find("or",sentence)
  175.    do write(i)
  176. .De
  177. which writes all the positions at which \*M"or"\fR occurs in \*Msentence\fR.
  178. For the example above, these are 3, 23, and 33.
  179. .PP
  180. Generation is inherited like failure, and this expression can be written
  181. more concisely by omitting the optional \*Mdo\fR clause:
  182. .Ds
  183. every write(find("or",sentence))
  184. .De
  185. .PP
  186. There are several built-in generators in Icon. One of the most frequently
  187. used of these is
  188. .Ds
  189. i to j
  190. .De
  191. which generates the integers from \*Mi\fR to \*Mj\fR. This generator can be
  192. combined with \*Mevery-do\fR to formulate the traditional \*Mfor\fR-style
  193. control structure:
  194. .Ds
  195. every k := i to j do
  196.    f(k)
  197. .De
  198. Note that this expression can be written more compactly as
  199. .Ds
  200. every f(i to j)
  201. .De
  202. .PP
  203. There are a number of other control structures related to generation.
  204. One is \fIalternation\fR,
  205. .Ds
  206. \*1 | \*2
  207. .De
  208. which generates the results of \*1 followed by the results of \*2.
  209. Thus
  210. .Ds
  211. every write(find("or",sentence1) | find("or",sentence2))
  212. .De
  213. writes the positions of \*M"or"\fR in \*Msentence1\fR followed by
  214. the positions of \*M"or"\fR in \*Msentence2\fR. Again, this sentence can
  215. be written more compactly by using alternation in the second
  216. argument of \*Mfind()\fR:
  217. .Ds
  218. every write(find("or",sentence1 | sentence2))
  219. .De
  220. .PP
  221. Another use of alternation is illustrated by
  222. .Ds
  223. (i | j | k) = (0 | 1)
  224. .De
  225. which succeeds if any of \*Mi\fR, \*Mj\fR, or \*Mk\fR has the value 0 or 1.
  226. .PP
  227. Procedures can be used to add generators to Icon's built-in repertoire. For example,
  228. .Ds
  229. procedure findodd(s1,s2)
  230.    every i := find(s1,s2) do
  231.       if i % 2 = 1 then suspend i
  232. end
  233. .De
  234. is a procedure that generates the odd-valued positions at which \*Ms1\fR occurs
  235. in \*Ms2\fR. The \*Msuspend\fR control structure returns a value from the
  236. procedure, but leaves it in suspension so that it can be resumed for another
  237. value. When the loop terminates, control flows off the end of the
  238. procedure without producing another value.
  239. .NH
  240. String Scanning
  241. .PP
  242. For complicated operations, the bookkeeping involved in keeping track of
  243. positions in strings becomes burdensome and error prone.
  244. Icon has a string scanning facility that is
  245. manages positions automatically.
  246. Attention is
  247. focused on a current position in a string as it is examined by a sequence of
  248. operations.
  249. .PP
  250. The string scanning operation has the form
  251. .Ds
  252. s ? \*0
  253. .De
  254. where \*Ms\fR is the \fIsubject\fR string to be examined and \*0 is an expression that
  255. performs the examination.
  256. A position in the subject, which starts at 1, is the focus of examination.
  257. .PP
  258. \fIMatching functions\fR change this position.
  259. One matching function, \*Mmove(i)\fR, moves the position by \*Mi\fR and
  260. produces the substring of the subject between the previous and new
  261. positions. If the position cannot be moved by the specified amount
  262. (because the subject is not long enough), \*Mmove(i)\fR fails. A
  263. simple example is
  264. .Ds
  265. line ? while write(move(2))
  266. .De
  267. which writes successive two-character substrings of \*Mline\fR, stopping
  268. when there are no more characters.
  269. .PP
  270. Another matching function is \*Mtab(i)\fR, which sets the position in the
  271. subject to \*Mi\fR and also returns the substring of the subject between
  272. the previous and new positions.
  273. For example,
  274. .Ds
  275. line ? if tab(10) then write(tab(0))
  276. .De
  277. first sets the position in the subject to 10 and then to the end of the subject, writing
  278. \*Mline\^[10:0]\fR.
  279. Note that no value is written if the subject is not long enough.
  280. .PP
  281. String analysis functions such as \*Mfind()\fR
  282. can be used in string scanning. In this context, the string that they
  283. operate on is not specified and is taken to be the subject. For example,
  284. .Ds
  285. line ? while write(tab(find("or")))
  286.    do move(2)
  287. .De
  288. writes all the substrings of \*Mline\fR prior to occurrences of \*M"or"\fR.
  289. Note that \*Mfind()\fR produces a position, which is then used by \*Mtab\fR
  290. to change the position and produce the desired substring. The \*Mmove(2)\fR
  291. skips the \*M"or"\fR that is found.
  292. .PP
  293. Another example of the use of string analysis functions in scanning is
  294. .Ds
  295. line ? while tab(upto(&letters)) do
  296.    write(tab(many(&letters)))
  297. .De
  298. which writes all the words in \*Mline\fR.
  299. .PP
  300. As illustrated in the examples above, any expression may occur in
  301. the scanning expression.
  302. .NH
  303. Structures
  304. .PP
  305. Icon supports several kinds of structures with different organizations
  306. and access methods. Lists are linear structures that can be accessed
  307. both by position and by stack and queue functions. Sets are collections
  308. of arbitrary values with no implied ordering. Tables provide an
  309. associative lookup mechanism.
  310. .NH 2
  311. Lists
  312. .PP
  313. While strings are sequences of characters, lists in Icon are sequences
  314. of values of arbitrary types. Lists are created by enclosing the lists
  315. of values in brackets. An example is
  316. .Ds
  317. car1 := ["buick","skylark",1978,2450]
  318. .De
  319. in which the list \*Mcar1\fR has four values, two of which are strings
  320. and two of which are integers. Note that the values in a list need not
  321. all be of the same type. In fact, any kind of value can occur in a list
  322. \(em even another list, as in
  323. .Ds
  324. inventory := [car1,car2,car3,car4]
  325. .De
  326. .PP
  327. Lists also can be created by
  328. .Ds
  329. L := list(i,x)
  330. .De
  331. which creates a list of \*Mi\fR values, each of which has the value
  332. \*Mx\fR.
  333. .PP
  334. The values in a list can be referenced by position much like the
  335. characters in a string. Thus
  336. .Ds
  337. car1\^[4] := 2400
  338. .De
  339. changes the last value in \*Mcar1\fR to 2400.
  340. A reference that is out of the range of the list fails. For example,
  341. .Ds
  342. write(car1\^[5])
  343. .De
  344. fails.
  345. .PP
  346. The values in a list \*ML\fR are generated by \*M!L\fR. Thus
  347. .Ds
  348. every write(!L)
  349. .De
  350. writes all the values in \*ML\fR.
  351. .PP
  352. Lists can be manipulated like stacks and queues. The function
  353. \*Mpush(L,x)\fR
  354. adds the value of \*Mx\fR to the left end of the list \*ML\fR,
  355. automatically increasing the size of \*ML\fR by one. Similarly,
  356. \*Mpop(L)\fR removes the leftmost value from \*ML\fR, automatically
  357. decreasing the size of \*ML\fR by one, and produces the removed value.
  358. .NH 2
  359. Sets
  360. .PP
  361. A sets is a collection of values.
  362. An empty set is created by \*Mset()\fR.
  363. Alternatively, \*Mset(L)\fR produces a set with the values in the list \*ML\fR.
  364. For example,
  365. .Ds
  366. S := set(\^[1,"abc",[\^]\^])
  367. .De
  368. assigns to \*MS\fR a set that contains the integer 1, the string \*M"abc"\fR,
  369. and an empty list.
  370. .PP
  371. The set operations of union, intersection, and difference are provided.
  372. The function \*Mmember(S,x)\fR succeeds if \*Mx\fR is a member of the
  373. set \*MS\fR but fails otherwise. The function \*Minsert(S,x)\fR
  374. adds \*Mx\fR to the set \*MS\fR,
  375. while \*Mdelete(S,x)\fR removes \*Mx\fR from \*MS\fR. A value only can occur once in
  376. a set, so \*Minsert(S,x)\fR has no effect if \*Mx\fR is already in
  377. \*MS\fR.
  378. \*M!S\fR generates the members of \*MS\fR.
  379. .PP
  380. A simple example of the use of sets is given by the following
  381. segment of code, which lists all the different words that
  382. appear in the input file:
  383. .Ds
  384. words := set()
  385. while line := read() do
  386.    line ? while tab(upto(&letters)) do
  387.       insert(words,tab(many(&letters)))
  388. every write(!words)
  389. .De
  390. .NH 2
  391. Tables
  392. .PP
  393. Tables are sets of pairs each of which consists of a key and a corresponding
  394. value. The key and its corresponding value may be of any type,
  395. and the value for any key can be looked up automatically.
  396. Thus, tables provide a form of associative access in contrast with the
  397. positional access to values in lists.
  398. .PP
  399. A table is created by an expression such as
  400. .Ds
  401. symbols := table(0)
  402. .De
  403. which assigns to \*Msymbols\fR a table with the default value 0.
  404. The default value is used for keys that are not assigned another value.
  405. Subsequently, \*Msymbols\fR can be referenced by any key, such as
  406. .Ds
  407. symbols\^["there"] := 1
  408. .De
  409. which assigns the value 1 to the key \*M"there"\fR in \*Msymbols\fR.
  410. .PP
  411. Tables grow automatically as new keys are added.
  412. For example, the following program segment produces a
  413. table containing a
  414. count of the
  415. words that appear in the input file:
  416. .Ds
  417. words := table(0)
  418. while line := read() do
  419.    line ? while tab(upto(&letters)) do
  420.       words\^[tab(many(&letters))] +:= 1
  421. .De
  422. Here the default value for each word is 0, as given
  423. in \*Mtable(0)\fR, and \*M+:=\fR is an augmented assignment operation that
  424. increments the values by one.
  425. There are augmented assignment operations for all binary operators.
  426. .PP
  427. A list can be obtained from a table \*MT\fR by the function \*Msort(T,1)\fR.
  428. The form of the list depends on the value of \*Mi\fR. For example, if
  429. \*Mi\fR is 3, the list contains alternate
  430. keys and their corresponding values in \*MT\fR.
  431. For example,
  432. .Ds
  433. wordlist := sort(words,3)
  434. while write(pop(wordlist)," : ",pop(wordlist))
  435. .De
  436. writes the words and their counts from \*Mwords\fR.
  437. .NH
  438. An Example
  439. .PP
  440. The following program, which produces a concordance of the words from an
  441. input file, illustrates typical Icon programming techniques. Although not all
  442. the features in this program are described in previous sections, the
  443. general idea should be clear.
  444. .de Ta
  445. .ta 3.i
  446. ..
  447. .Ds
  448. global uses, lineno, width
  449.  
  450. procedure main(args)
  451.    width := 15    # width of word field
  452.    uses := table()
  453.    lineno := 0
  454.    every tabulate(words())    # tabulate all the citations
  455.    output()    # print the citations
  456. end
  457. .Dd
  458. #  Add line number to citations for word
  459. #
  460. procedure tabulate(word)
  461.    /uses\^[word] := table()
  462.    uses\^[word]\^[lineno] := 1
  463.    return
  464. end
  465. .Dd
  466. #  Generate words
  467. #
  468. procedure words()
  469.    while line := read() do {
  470.       lineno +:= 1
  471.       write(right(lineno,6),"  ",\*bline)
  472.       map(line) ? while tab(upto(&letters)) do {
  473.          s := tab(many(&letters))
  474.          if *s < 3 then next    # skip short words
  475.          suspend s
  476.          }
  477.       }
  478. end
  479. .Dd
  480. #  Print the results
  481. #
  482. procedure output()
  483.    write()    # blank line
  484.    uses := sort(uses,3)    # sort citations
  485.    while word := get(uses) do {
  486.       line := ""
  487.       numbers := sort(get(uses),3)
  488.       while line ||:= get(numbers) || ", " do
  489.          get(numbers)    # skip marking value
  490.       write(left(word,width),\*bline\^[1:-2])
  491.       }
  492. end
  493. .De
  494. The program reads a line, writes it out with an identifying line number,
  495. and processes every word in the line. Words less than three characters
  496. long are considered to be ``noise'' and are discarded. A table,
  497. \*Muses\fR, is keyed by the words. Every key has a corresponding set
  498. of line numbers. The first time a word is encountered, a new set is created
  499. for it. The line number is inserted in any event. Since a value can be in a set
  500. only once, duplicate line numbers are suppressed automatically.
  501. .PP
  502. After all the input has been read, the table of words is sorted by key.
  503. Each corresponding set of line numbers is sorted and the line numbers
  504. are appended to the line to be written.
  505. .PP
  506. For example, if the input file is
  507. .Ds
  508.          On the Future!-how it tells
  509.          Of the rapture that impells
  510.         To the swinging and the ringing
  511.          Of the bells, bells, bells-
  512.       Of the bells, bells, bells, bells,
  513.                 Bells, bells, bells-
  514.   To the rhyming and the chiming of the bells!
  515. .De
  516. the output is
  517. .Ds
  518. .ta .7i
  519.     1           On the Future!-how it tells
  520.     2           Of the rapture that impells
  521.     3          To the swinging and the ringing
  522.     4           Of the bells, bells, bells-
  523.     5        Of the bells, bells, bells, bells,
  524.     6                  Bells, bells, bells-
  525.     7    To the rhyming and the chiming of the bells!
  526.  
  527. .ta 1i
  528. and    3, 7
  529. bells    4, 5, 6, 7
  530. chiming    7
  531. future    1
  532. how    1
  533. impells    2
  534. rapture    2
  535. rhyming    7
  536. ringing    3
  537. swinging    3
  538. tells    1
  539. that    2
  540. the    1, 2, 3, 4, 5, 7
  541. .De
  542. .SH
  543. Acknowledgement
  544. .PP
  545. Icon was designed by the the author in collaboration with Dave Hanson,
  546. Tim Korb, Cary Coutant, and Steve Wampler. Many other persons have
  547. contributed to its development. The current implementation is
  548. based on the work of Cary Coutant and Steve Wampler with recent
  549. contributions by Bill Mitchell, Janalee O'Bagy, Gregg Townsend, and
  550. Ken Walker.
  551. .[]
  552.